package com.nut.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;
import java.util.Set;

import com.nut.common.Result;
import com.nut.entity.MemberInviteLog;
import com.nut.mapper.MemberInviteLogMapper;
import com.nut.service.MemberInviteLogService;

@Service
@Slf4j
public class MemberInviteLogServiceImpl implements MemberInviteLogService {

    @Autowired
    private MemberInviteLogMapper memberInviteLogMapper;

    @Override
    public Result<IPage<MemberInviteLog>> listPage(Integer pageNo, Integer pageSize) {

        Page<MemberInviteLog> page = new Page<>(pageNo, pageSize);
        List<MemberInviteLog> list = memberInviteLogMapper.listPage(page, 1);
        page.setRecords(list);
        return Result.ok(page);
    }

    @Override
    public Result<IPage<MemberInviteLog>> selectPage(Integer pageNo, Integer pageSize) {

        Page<MemberInviteLog> page = new Page<>(pageNo, pageSize);
        LambdaQueryWrapper<MemberInviteLog> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.orderByDesc(MemberInviteLog::getId);

        IPage<MemberInviteLog> list = memberInviteLogMapper.selectPage(page, queryWrapper);
        return Result.ok(list);
    }

    @Override
    public Result<MemberInviteLog> getById(Serializable id) {
        MemberInviteLog memberInviteLog = memberInviteLogMapper.selectById(id);
        return Result.ok(memberInviteLog);
    }

    @Override
    public Result add(MemberInviteLog memberInviteLog) {
        memberInviteLogMapper.insert(memberInviteLog);
        return Result.ok();
    }

    @Override
    public Result updateById(MemberInviteLog memberInviteLog) {
        memberInviteLogMapper.updateById(memberInviteLog);
        return Result.ok();
    }

    @Override
    public Result deleteByIds(Set<Integer> ids) {
        for (Integer id : ids) {
            memberInviteLogMapper.deleteById(id);
        }
        return Result.ok();
    }

    @Override
    public void add(Long fromMemberId, Long toMemberId) {
        MemberInviteLog log = new MemberInviteLog();
        log.setFromMemberId(fromMemberId);
        log.setToMemberId(toMemberId);
        memberInviteLogMapper.insert(log);
    }
}